package com.labirynt.impl;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.labirynt.GameObject;
import com.labirynt.MechanicGame;






public class Player extends GameObject  {
	private ArrayList<Bitmap> alBmpPlayer1 = new ArrayList <Bitmap>() ;
	
	 private Paint textPaint;
	 int newXpos;
	 int newYpos;
	 
	public Player(String name,ArrayList<String>   alAnimBmp, int x, int y,int height, int width, Context ctx) 
	{
		super(name,x, y,(int) x+120,(int) y+132);
		LoadAnimation(alAnimBmp, ctx);
		this.Initialize(100,1, 4, false);
	//	this.setmBmpPlayer1(mBmpPlayer1);
		
		///frameTimer = 0;
		//currentFrame = 0;
		this.innitMechanicGme(50, 10);
		 textPaint = new Paint();
         textPaint.setARGB(255,255,0,115);
         textPaint.setTextSize(12);
         newXpos=0;
         newYpos=0;
	}
	
	
	private void LoadAnimation(ArrayList<String> alAnimBmp, Context ctx) {
		try
		  {
			for(String animBmp :alAnimBmp )
				this.alBmpPlayer1.add(BitmapFactory.decodeStream(ctx.getAssets().open(animBmp+".png"))); 
		} 
	    catch (IOException e) { 
	    	e.printStackTrace(); 
	    }
		
	}


	public void SetPos(int x,int y)
	{
		  newXpos=x;
	      newYpos=y;
		
		
	}
	
	
	@Override
	public void Update(long gameTime)
	{
		if (gameTime > frameTimer + fps) {
			frameTimer = gameTime;
			currentFrame += 1;

			if (currentFrame >= numFrames) {
				currentFrame = 0;

				if (!loop)
					dispose = true;

			}

			sRectangle.left =0;// currentFrame * spriteWidth; 
			sRectangle.right =spriteWidth ;//sRectangle.left + spriteWidth;
		}
		if(xPos < newXpos && newXpos-xPos>3 )
		this.xPos+=10;
		else 	if(xPos > newXpos && xPos -newXpos>3)
			this.xPos-=10;
		if(yPos<newYpos && newYpos- yPos>3)
		this.yPos+=10;
		else if(yPos>newYpos &&  yPos-newYpos>3)
			this.yPos-=10;
		
				//this.setYPos(y);
	}

	 
	@Override
	 public void draw(Canvas canvas)
	{
	
		if(this.getMechanicGame().IsNotDead())
		{
			
				  int width=-120/2;//canvas.getWidth();	
				  int heigh =-132;//canvas.getHeight();
			Rect dest = new Rect(width+getXPos(),heigh+ getYPos(), width+getXPos() + spriteWidth,
					heigh+getYPos() + spriteHeight);
	
			canvas.drawBitmap(alBmpPlayer1.get(currentFrame), sRectangle, dest, null);
			Paint RectPaint = new Paint();
			RectPaint.setColor(Color.DKGRAY);		
			canvas.drawRect(width+getXPos()-10, heigh+getYPos(),width+getXPos()+40, heigh+getYPos()-10, RectPaint);
			RectPaint.setColor(Color.CYAN);
			canvas.drawRect(width+getXPos()-5, heigh+getYPos()-2,width+getXPos()+(this.getMechanicGame().iHitPoints-5)/2, heigh+ getYPos()-8, RectPaint);
	        canvas.drawText(obiektName ,width+getXPos()-20,heigh+ getYPos()-25, textPaint);
		}
		else
		{
			  textPaint.setTextSize(64);
			canvas.drawText("Game over" ,0,canvas.getHeight()/2, textPaint);
			
		}
	}



	 public boolean isCollision(int x,int y)
	  {
		  return ! (( x <this.spriteWidth
		    		&& x>this.xPos)
		    		||( y < this.yPos
		    		&& y > this.spriteHeight
		    		));
		  
	  }
	  
	  public boolean isCollisionWith(GameObject other)  
	  {
	    return  intersectRect( other);
	  }





	




}